<template>
    <div class="login_containter">
        <div class="login_box">
                <div class="avater_box">
                    <img src="../assets/4.jpg" alt="">
                </div>
                <!-- 为什么要用model绑定？？？   因为获得的是一个表单对象，后期表单校验需要使用这个表单对象    -->
             <el-form ref="loginFormRef" class="login_form" :rules="rules" :model="form" label-width="0px">
                    <el-form-item  prop="username">
                        <el-input  v-model="form.username" prefix-icon="iconfont icon-user"  clearable></el-input>
                    </el-form-item>
                     <el-form-item prop="password" >
                        <el-input type="password" show-password v-model="form.password" prefix-icon="iconfont icon-3702mima" ></el-input>
                    </el-form-item>
                     <el-form-item  class="btns">
                       <el-button type="primary" @click="login">登录</el-button>
                        <el-button type="info" @click="reset">重置 </el-button>
                        <!--element已经封装过这个click事件  如果没有可以 可以后面加.native -->
                    </el-form-item>
                </el-form>
            </div> 
               
    </div>
</template>
<script>
export default {
    data(){
        return {
            form:{
                username:'admin',
                password:'123456'
            },
              rules: {
          username: [
            { required: true, message: '请输入姓名', trigger: 'blur' },
            { min: 3, amax: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
           password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, amax: 10, message: '长度在 6到 10个字符', trigger: 'blur' }
          ],
          }

        }
         
    },
    methods:{
        reset(){

            this.$refs.loginFormRef.resetFields();
        },login(){
            ////表单的第二次验证 ，点击登录按钮  
            this.$refs.loginFormRef.validate(async (res)=>{
                if(!res) return ;
            const { data: result}= await this.$http.post('login',this.form)
            
                  if(result.meta.status!=200) {
                    this.$message.error('登录失败');
                    this.$refs.loginFormRef.resetFields();
                  } else {
                    this.$message.success('登陆成功 ')
                  console.log(result);
                  ///////将token值存储在sessionStorage中
                  window.sessionStorage.setItem('token',result.data.token);
                ///////路由导航到home组件
                  this.$router.push('/Home')
                  }
              
            });
        }
    }
    

}
</script>
<style lang="less" scoped>
    .login_containter {
        
        background-color: #2b4b6b;
        height: 100%;
    }
    .login_box {
     
        width: 450px;
        height: 300px;
        background-color: #fff;
        border-radius: 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-50%);
        .avater_box {
            width: 130px;
            height: 130px;
            position: absolute;
            left: 50%;
            transform: translate(-50%,-50%);
            border-radius: 50%;
            padding: 10px;
            background-color: #fff;
            border:1px solid #eee;
            box-shadow: 0 0 10px #eee;
             img {
                 width: 100%;
                 height: 100%;
                 border-radius: 50%;
             }
        }
    }
    .login_form {
        position: absolute;
        bottom: 0;
        padding: 0 20px;
        box-sizing: border-box;
        width: 100%;
       
      
    }
    .btns {
        display: flex;
         justify-content: flex-end;
    }
</style>